#!/bin/bash

# 脚本功能：自动打包Spring Boot基座项目和插件项目，并将它们复制到app目录下对应位置

# 设置颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 打印带颜色的信息
print_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查命令执行结果
check_result() {
    if [ $? -ne 0 ]; then
        print_error "$1"
        exit 1
    fi
}

# 设置项目路径
PROJECT_ROOT="/home/daoos/IdeaProjects/xjar-plus"
SPRING_PROJECTS="$PROJECT_ROOT/spring-projects"
APP_DIR="$PROJECT_ROOT/app/encrypted"
PLUGINS_DIR="$PROJECT_ROOT/plugins"

# 创建必要的目录
print_info "创建必要的目录"
mkdir -p "$APP_DIR/resource"
mkdir -p "$APP_DIR/plugins"
check_result "无法创建目录"

# 打包基座项目
print_info "打包基座项目"
cd "$SPRING_PROJECTS/base-project"
mvn clean package
check_result "基座项目打包失败"

# 打包插件A
print_info "打包插件A"
cd "$SPRING_PROJECTS/plugin-a"
mvn clean package
check_result "插件A打包失败"

# 打包插件B
print_info "打包插件B"
cd "$SPRING_PROJECTS/plugin-b"
mvn clean package
check_result "插件B打包失败"

# 复制生成的JAR文件到目标位置
print_info "复制生成的JAR文件到目标位置"
cd "$SPRING_PROJECTS/base-project"
BASE_JAR=$(find target -name "*.jar" -type f | head -n 1)
cp "$BASE_JAR" "$APP_DIR/resource/app.jar"
check_result "无法复制基座项目JAR文件"

cd "$SPRING_PROJECTS/plugin-a"
PLUGIN_A_JAR=$(find target -name "*.jar" -type f | head -n 1)
cp "$PLUGIN_A_JAR" "$PLUGINS_DIR/plugin-a-0.0.1-SNAPSHOT.jar"
check_result "无法复制插件A JAR文件"

cd "$SPRING_PROJECTS/plugin-b"
PLUGIN_B_JAR=$(find target -name "*.jar" -type f | head -n 1)
cp "$PLUGIN_B_JAR" "$PLUGINS_DIR/plugin-b-0.0.1-SNAPSHOT.jar"
check_result "无法复制插件B JAR文件"

print_info "所有项目打包完成！"
print_info "基座项目已复制到: $APP_DIR/resource/app.jar"
print_info "插件已复制到: $PLUGINS_DIR/"